{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Fraction to Recurring Decimal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: fractionToDecimal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #分数到小数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个整数，分别表示分数的分子&nbsp;<code>numerator</code> 和分母 <code>denominator</code>，以 <strong>字符串形式返回小数</strong> 。</p>\n",
    "\n",
    "<p>如果小数部分为循环小数，则将循环的部分括在括号内。</p>\n",
    "\n",
    "<p>如果存在多个答案，只需返回 <strong>任意一个</strong> 。</p>\n",
    "\n",
    "<p>对于所有给定的输入，<strong>保证</strong> 答案字符串的长度小于 <code>10<sup>4</sup></code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>numerator = 1, denominator = 2\n",
    "<strong>输出：</strong>\"0.5\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>numerator = 2, denominator = 1\n",
    "<strong>输出：</strong>\"2\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>numerator = 4, denominator = 333\n",
    "<strong>输出：</strong>\"0.(012)\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>-2<sup>31</sup> &lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "\t<li><code>denominator != 0</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [fraction-to-recurring-decimal](https://leetcode.cn/problems/fraction-to-recurring-decimal/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [fraction-to-recurring-decimal](https://leetcode.cn/problems/fraction-to-recurring-decimal/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n2', '2\\n1', '4\\n333']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, n: int, d: int) -> str:\n",
    "        if n == 0:\n",
    "            return \"0\"\n",
    "\n",
    "        digits: List[str] = []\n",
    "        \n",
    "        if (n < 0 and d > 0) or (n > 0 and d < 0):\n",
    "            digits.append(\"-\")\n",
    "        n = abs(n)\n",
    "        d = abs(d)\n",
    "        \n",
    "        digits.append(str(n // d))\n",
    "        n %= d\n",
    "        if n == 0:\n",
    "            return \"\".join(digits)\n",
    "        digits.append(\".\")\n",
    "        \n",
    "        r_pos: Dict[int, int] = dict()\n",
    "        pos = len(digits)\n",
    "        r_pos[n] = pos\n",
    "        pos += 1\n",
    "        n *= 10\n",
    "        while n != 0:\n",
    "            while n < d:\n",
    "                digits.append(\"0\")\n",
    "                pos += 1\n",
    "                n *= 10\n",
    "            digits.append(str(n // d))\n",
    "            n %= d\n",
    "            \n",
    "            if n in r_pos:\n",
    "                break\n",
    "            if n == 0:\n",
    "                break\n",
    "                \n",
    "            r_pos[n] = pos\n",
    "            # print(f\"{n}/{d} {digits}\")\n",
    "            # print(r_pos)\n",
    "            n *= 10\n",
    "            pos += 1\n",
    "\n",
    "        \n",
    "        if n == 0:\n",
    "            ans = \"\".join(digits)\n",
    "        else:\n",
    "            ans = \"\".join(\n",
    "                digits[:r_pos[n]] + [\"(\"] + digits[r_pos[n]:] + [\")\"]\n",
    "            )\n",
    "            \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if numerator==0:return '0'\n",
    "        if numerator*denominator<0:symbol='-'\n",
    "        else:symbol=\"\"\n",
    "        numerator=abs(numerator)\n",
    "        denominator=abs(denominator)\n",
    "        if numerator%denominator==0:\n",
    "            return symbol+str(numerator//denominator)\n",
    "        d=denominator\n",
    "        #判断是否能除尽\n",
    "        while d%5==0:\n",
    "            d//=5\n",
    "        while d%2==0:\n",
    "            d//=2\n",
    "        if d==1:\n",
    "            str1=symbol+str(numerator//denominator)+'.'\n",
    "            str2=\"\"\n",
    "            y=numerator%denominator*10\n",
    "            while y:\n",
    "                str2+=str(y//denominator)\n",
    "                y=y%denominator*10\n",
    "            return str1+str2\n",
    "            \n",
    "        before=str(int(numerator/denominator))\n",
    "        y=numerator%denominator\n",
    "        x=denominator\n",
    "        decimal=[y]\n",
    "        str1=\"\"\n",
    "        while y*10%x not in decimal:\n",
    "            decimal.append(y*10%x)\n",
    "            str1+=str(y*10//x)\n",
    "            y=y*10%x\n",
    "        index=decimal.index(y*10%x)\n",
    "        str1=str1[:index]+'('+str1[index:]+str(y*10//x)+')'\n",
    "        return symbol+before+\".\"+str1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        negative = False\n",
    "        if numerator==0:\n",
    "            return \"0\"\n",
    "        if (numerator>0) ^ (denominator>0):\n",
    "            negative = True\n",
    "        \n",
    "        numerator = numerator if numerator>=0 else -numerator\n",
    "        denominator = denominator if denominator>=0 else -denominator\n",
    "        numPart = numerator // denominator\n",
    "        extra = numerator % denominator\n",
    "        \n",
    "        extraList = []\n",
    "        fracPart = \"\"\n",
    "        while extra!=0:\n",
    "            extra *= 10\n",
    "            if extra in extraList:\n",
    "                repeatLen = len(extraList) - extraList.index(extra)\n",
    "                fracPart = fracPart[:-repeatLen] + \"(\" + fracPart[-repeatLen:] + \")\"\n",
    "                break\n",
    "            extraList.append(extra)\n",
    "            \n",
    "            if extra < denominator:\n",
    "                fracPart += \"0\"\n",
    "                continue\n",
    "            \n",
    "            frac = extra // denominator\n",
    "            fracPart += str(frac)\n",
    "            extra = extra % denominator\n",
    "            \n",
    "        result = str(numPart)\n",
    "        if negative:\n",
    "            result = \"-\" + result\n",
    "        if fracPart != \"\":\n",
    "            result += \".\" + fracPart\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        sign = (numerator * denominator) >= 0\n",
    "        numerator, denominator = abs(numerator), abs(denominator)\n",
    "        \n",
    "        q, r = divmod(numerator, denominator)\n",
    "        \n",
    "        res = str(q)\n",
    "        if not sign:\n",
    "            res = \"-\" + res\n",
    "        \n",
    "        if r == 0:\n",
    "            return res\n",
    "        \n",
    "        l = []\n",
    "        dic = {}\n",
    "        r = r * 10\n",
    "        while r != 0:\n",
    "            tmp, r = divmod(r, denominator)\n",
    "            if r in dic and tmp == dic[r][0]:\n",
    "                break\n",
    "            else:\n",
    "                dic[r] = [tmp, len(l)]\n",
    "            l.append(str(tmp))\n",
    "            r *= 10\n",
    "        \n",
    "        res += \".\"\n",
    "        if r == 0:\n",
    "            return res + \"\".join(l)\n",
    "        else:\n",
    "            for i in range(len(l)):\n",
    "                if i == dic[r][1]:\n",
    "                    res += '('\n",
    "                res += str(l[i])\n",
    "            res += ')'\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        d = dict()\n",
    "        div, mod = self.divmod(numerator, denominator)\n",
    "        if mod == 0:\n",
    "            return str(div)\n",
    "        ans = \"-\" if ((numerator > 0) ^ (denominator > 0)) else \"\"\n",
    "        div, mod, denominator = abs(div), abs(mod), abs(denominator)\n",
    "        ans += str(div) + \".\"\n",
    "        d[mod] = len(ans)\n",
    "        while mod:\n",
    "            mod *= 10\n",
    "            div, mod = self.divmod(mod, denominator)\n",
    "            ans += str(div)\n",
    "            if mod in d:\n",
    "                index = d[mod]\n",
    "                ans = ans[:index] + \"(\" + ans[index:] + \")\"\n",
    "                break\n",
    "            else:\n",
    "                d[mod] = len(ans)\n",
    "        return ans\n",
    "\n",
    "    def divmod(self, a, b):\n",
    "        q = int(a / b)   # I'm using 3.x\n",
    "        r = a - b * q\n",
    "        return (q, r)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        neg = False\n",
    "        if numerator < 0 and denominator >= 0:\n",
    "            numerator = -numerator\n",
    "            neg = True\n",
    "        elif numerator >=0 and denominator < 0:\n",
    "            denominator = -denominator\n",
    "            neg = True\n",
    "        r = numerator % denominator\n",
    "        if neg and numerator != 0:\n",
    "            num = '-' + str(numerator // denominator)\n",
    "        else:\n",
    "            num = str(numerator // denominator)\n",
    "        if r == 0:\n",
    "            return num\n",
    "        res = []\n",
    "        rs = {}\n",
    "        while True:\n",
    "            if r == 0:\n",
    "                return num + '.' + ''.join(res)\n",
    "            if r in rs:\n",
    "                pos = rs[r]\n",
    "                return num + '.' + ''.join(res[:pos]) + '(' + ''.join(res[pos:]) +')'\n",
    "            rs[r] = len(res)\n",
    "            r *= 10\n",
    "            res.append(str(r // denominator))\n",
    "            r %= denominator\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if denominator == 0:\n",
    "            return ''\n",
    "        num, deno = abs(numerator), abs(denominator)\n",
    "        remainder = {}\n",
    "        count = 0\n",
    "        symbol = '' if numerator*denominator >= 0 else '-'\n",
    "        intPart, remainderPre, decimal = num//deno, num%deno, ''\n",
    "        remainder[remainderPre] = count\n",
    "        count += 1\n",
    "        while remainderPre != 0:\n",
    "            quotient, remainderPre = remainderPre * 10 // deno, remainderPre * 10 % deno\n",
    "            if remainderPre not in remainder:\n",
    "                decimal += str(quotient)\n",
    "                remainder[remainderPre] = count\n",
    "                count += 1\n",
    "            else:\n",
    "                decimal += str(quotient)\n",
    "                decimal = decimal[:remainder[remainderPre]] + '(' + decimal[remainder[remainderPre]:] + ')'\n",
    "                break\n",
    "        point = '.' if len(decimal) > 0 else ''\n",
    "        return symbol + str(intPart) + point + decimal\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        # if numerator == 0:\n",
    "        #     return '0'\n",
    "        # if denominator == 1:\n",
    "        #     return str(numerator)\n",
    "        # res = []\n",
    "        # # if (numerator > 0) ^ (denominator > 0):\n",
    "        # #     res.append('-')\n",
    "        # if (numerator > 0) ^ (denominator > 0):\n",
    "        #     res.append(\"-\")\n",
    "        # numerator, denominator = abs(numerator), abs(denominator)\n",
    "        # a, b= divmod(numerator, denominator)\n",
    "        # if b == 0:\n",
    "        #     return str(a)\n",
    "        # res.append(str(a))\n",
    "        # res.append('.')\n",
    "        # loc = {b: len(res)}\n",
    "        # while b:\n",
    "        #     a = b*10\n",
    "        #     a, b = divmod(a, denominator)\n",
    "        #     res.append(str(a))\n",
    "        #     if b in loc:\n",
    "        #         res.insert(loc[b], '(')\n",
    "        #         res.append(')')\n",
    "        #         break\n",
    "        #     loc[b] = len(res)\n",
    "        # return ''.join(res)\n",
    "        if numerator == 0: return \"0\"\n",
    "        res = []\n",
    "        # 首先判断结果正负, 异或作用就是 两个数不同 为 True 即 1 ^ 0 = 1 或者 0 ^ 1 = 1\n",
    "        if (numerator > 0) ^ (denominator > 0):\n",
    "            res.append(\"-\")\n",
    "        numerator, denominator = abs(numerator), abs(denominator)\n",
    "        # 判读到底有没有小数\n",
    "        a, b = divmod(numerator, denominator)\n",
    "        res.append(str(a))\n",
    "        # 无小数\n",
    "        if b == 0:\n",
    "            return \"\".join(res)\n",
    "        res.append(\".\")\n",
    "        # 处理余数\n",
    "        # 把所有出现过的余数记录下来\n",
    "        loc = {b: len(res)}\n",
    "        while b:\n",
    "            b *= 10\n",
    "            a, b = divmod(b, denominator)\n",
    "            res.append(str(a))\n",
    "            # 余数前面出现过,说明开始循环了,加括号\n",
    "            if b in loc:\n",
    "                res.insert(loc[b], \"(\")\n",
    "                res.append(\")\")\n",
    "                break\n",
    "            # 在把该位置的记录下来\n",
    "            loc[b] = len(res)\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution(object):\n",
    "    def fractionToDecimal(self, numerator, denominator):\n",
    "        \"\"\"\n",
    "        :type numerator: int\n",
    "        :type denominator: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if numerator==0:\n",
    "            return \"0\"\n",
    "        res = []\n",
    "        if ((numerator>=0) is (denominator>=0)) == False:\n",
    "            res.append(\"-\")\n",
    "        numerator, denominator = abs(numerator),abs(denominator)\n",
    "        count = 0\n",
    "        a = -1\n",
    "        res.append(str(numerator//denominator))\n",
    "        res.append(\".\")\n",
    "        xiaoshu = []\n",
    "        numerator = numerator%denominator*10\n",
    "        while numerator and count<10:\n",
    "            if a>=0:\n",
    "                count += 1\n",
    "                if str(numerator//denominator)!=xiaoshu[a+count]:\n",
    "                    count = 0\n",
    "                    a = -1\n",
    "            if a==-1 and str(numerator//denominator) in xiaoshu:\n",
    "                a = xiaoshu.index(str(numerator//denominator))\n",
    "            xiaoshu.append(str(numerator//denominator))\n",
    "            numerator = numerator%denominator*10\n",
    "        if xiaoshu == []:\n",
    "            res.pop()\n",
    "        if numerator:\n",
    "            xiaoshu = xiaoshu[:-11]\n",
    "            while a>0 and xiaoshu[a-1] == xiaoshu[-1]:     #循环检测是否存在计数过程中的漏检\n",
    "                a = a-1\n",
    "                xiaoshu = xiaoshu[:-1]\n",
    "            xiaoshu.insert(a, \"(\")\n",
    "            xiaoshu.append(\")\")\n",
    "        res = res + xiaoshu\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        fuhao = False\n",
    "        if (numerator<0) ^ (denominator<0) and numerator!=0 and denominator!=0:\n",
    "            fuhao = True\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        intnum = str(numerator//denominator)\n",
    "        if fuhao:\n",
    "            intnum = '-'+intnum\n",
    "        if numerator%denominator == 0:\n",
    "            return intnum\n",
    "\n",
    "        allyu = set()\n",
    "        yu_float = []\n",
    "        yu = numerator%denominator\n",
    "        while yu!=0 and yu not in allyu:\n",
    "            allyu.add(yu)\n",
    "            yu*=10\n",
    "            floatres = yu//denominator\n",
    "            yu_float.append([yu//10, floatres])\n",
    "            yu = yu%denominator\n",
    "        \n",
    "        print(allyu)\n",
    "        print(yu_float)\n",
    "        print(yu)\n",
    "        floatnum = ''\n",
    "        i = 0\n",
    "        while i < len(yu_float):\n",
    "            if yu != yu_float[i][0]:\n",
    "                floatnum += str(yu_float[i][1])\n",
    "                i+=1\n",
    "            else:\n",
    "                break\n",
    "        if yu != 0:\n",
    "            floatnum+='('\n",
    "            while i < len(yu_float):\n",
    "                floatnum += str(yu_float[i][1])\n",
    "                i+=1\n",
    "            floatnum+=')'\n",
    "        return intnum+'.'+floatnum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0: return \"0\"\n",
    "        res = []\n",
    "        if (numerator>0) ^ (denominator>0): res.append(\"-\")\n",
    "        numerator, denominator = abs(numerator), abs(denominator)\n",
    "        a, b = divmod(numerator, denominator)\n",
    "        res.append(str(a))\n",
    "        if b == 0: return \"\".join(res)\n",
    "        res.append(\".\")\n",
    "        # 开始处理余数\n",
    "        loc = {b: len(res)}\n",
    "        while b:\n",
    "            b *= 10\n",
    "            a, b = divmod(b, denominator)\n",
    "            res.append(str(a))\n",
    "            # 余数前面出现过，说明开始循环了，加括号\n",
    "            if b in loc:\n",
    "                res.insert(loc[b], \"(\")\n",
    "                res.append(\")\")\n",
    "                break\n",
    "            # 再把该位置记录下来\n",
    "            loc[b] = len(res)\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str: \n",
    "        ans = ''\n",
    "        flag = False\n",
    "        if (numerator < 0 and denominator > 0) or (numerator > 0 and denominator < 0):\n",
    "            flag = True\n",
    "            numerator = -numerator\n",
    "        last = [numerator]\n",
    "        ans += str(numerator // denominator)\n",
    "        numerator %= denominator\n",
    "        if numerator != 0:\n",
    "            ans += '.'\n",
    "            while numerator != 0:\n",
    "                numerator *= 10\n",
    "                if numerator in last:\n",
    "                    k = last.index(numerator)\n",
    "                    if k == 0:\n",
    "                        ans = ans[:ans.index('.')+1] + '(' + ans[ans.index('.')+1:] + ans[:ans.index('.')] + ')'\n",
    "                    else:\n",
    "                        ans = ans[:ans.index('.')+k] + '(' + ans[ans.index('.')+k:] + ')'\n",
    "                    break\n",
    "                last.append(numerator)\n",
    "                ans += str(numerator // denominator)\n",
    "                numerator %= denominator\n",
    "        if flag:\n",
    "            return '-' + ans\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return \"0\"\n",
    "        res = []\n",
    "        if (numerator<0) ^ (denominator<0):\n",
    "            res.append('-')\n",
    "        numer, denom = abs(numerator), abs(denominator)\n",
    "        f, l = divmod(numer, denom)\n",
    "        res.append(str(f))\n",
    "        if l != 0:\n",
    "            res.append('.')\n",
    "        cur = dict()\n",
    "        while l != 0:\n",
    "            # if f in cur:\n",
    "            #     pass\n",
    "            if l in cur:\n",
    "                res.insert(cur[l], '(')\n",
    "                res.append(')')\n",
    "                break\n",
    "            cur[l] = len(res)\n",
    "            f, l = divmod(l * 10, denom)\n",
    "            res.append(str(f))\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        \n",
    "        fraction = []\n",
    "        if (numerator < 0) ^ (denominator < 0):\n",
    "            fraction.append('-')\n",
    "        \n",
    "        dividend = abs(numerator)\n",
    "        divisor = abs(denominator)\n",
    "        fraction.append(str(dividend // divisor))\n",
    "        remainder = dividend % divisor\n",
    "        if remainder == 0:\n",
    "            return ''.join(fraction)\n",
    "            \n",
    "        fraction.append('.')\n",
    "        memo = {}\n",
    "        while remainder != 0:\n",
    "            if remainder in memo:\n",
    "                fraction.insert(memo[remainder], '(')\n",
    "                fraction.append(')')\n",
    "                break\n",
    "            memo[remainder] = len(fraction)\n",
    "            remainder *= 10\n",
    "            fraction.append(str(remainder // divisor))\n",
    "            remainder %= divisor\n",
    "        return ''.join(fraction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "\n",
    "        def getXunhuanPos(p,q):\n",
    "            assert(p<q)\n",
    "            assert(q>=2)\n",
    "            root = 1\n",
    "            def getpower(n):\n",
    "                assert(n>1)\n",
    "                roott = 0\n",
    "                while(q%(n**roott)==0):\n",
    "                    roott+=1\n",
    "                return roott-1\n",
    "            roott2 = getpower(2)\n",
    "            roott5 = getpower(5)\n",
    "            return max(roott2, roott5)\n",
    "\n",
    "        def getXunhuanLen(q, mm):\n",
    "            root = mm+1\n",
    "            while((10**root - 10**mm)%q !=0):\n",
    "                root = root+1\n",
    "            return root-mm\n",
    "\n",
    "\n",
    "        def womendejie(p,q):\n",
    "            entire = int(p//q)\n",
    "            \n",
    "            if(p - q*entire==0):\n",
    "                return str(entire)\n",
    "            else:\n",
    "                entirepart = str(entire) + \".\"\n",
    "                p = p -q*entire\n",
    "            assert(0<p<q)\n",
    "            p, q = p/math.gcd(p,q), q/math.gcd(p,q)\n",
    "            p, q = int(p), int(q)\n",
    "            xunhuanpos = getXunhuanPos(p,q)\n",
    "            xunhuanlen = getXunhuanLen(q,xunhuanpos)\n",
    "            \n",
    "            #print(xunhuanpos,xunhuanlen)\n",
    "            \n",
    "            buxunhuan = (p*10**xunhuanpos)//q\n",
    "            xunhuanpart = (((p*10**xunhuanpos)%q)*10**(xunhuanlen))//q\n",
    "            \n",
    "            \n",
    "            \n",
    "            #print(buxunhuan, xunhuanpart)\n",
    "            if(int(buxunhuan)==0):\n",
    "                s1 = \"\"\n",
    "            else:\n",
    "                s1 = str(int(buxunhuan))\n",
    "            \n",
    "            if(xunhuanpart==0):\n",
    "                s2 = \"\"\n",
    "            else:\n",
    "                s2 =  str(int(xunhuanpart))\n",
    "                while(len(s2)<xunhuanlen):\n",
    "                    s2 =  \"0\" +s2\n",
    "            \n",
    "            remainning1 = int(xunhuanpos) - len(s1)\n",
    "\n",
    "            ss1, ss2 = entirepart, \"\"\n",
    "            for i in range(max(0,remainning1 )):\n",
    "                ss1+= \"0\"\n",
    "            ss1+=s1\n",
    "            \n",
    "            \n",
    "            if(s2):\n",
    "                ss1+=\"(\" + s2 + \")\"\n",
    "            \n",
    "            return ss1\n",
    "        if(numerator==0):\n",
    "            return \"0\"\n",
    "        if(numerator>0 and denominator>0):\n",
    "            return womendejie(numerator, denominator)\n",
    "        if(numerator>0 and denominator<0):\n",
    "            return \"-\" + womendejie(numerator, -denominator)\n",
    "        if(numerator<0 and denominator>0):\n",
    "            return \"-\" + womendejie(-numerator, denominator)\n",
    "        if(numerator<0 and denominator,0):\n",
    "            return womendejie(-numerator, -denominator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        # 1. 正负号\n",
    "        # 2. 整除\n",
    "        # 3. 小数\n",
    "        # 4. 循环小数\n",
    "\n",
    "        if numerator == 0:\n",
    "            return \"0\"\n",
    "\n",
    "        res = []\n",
    "        if ( numerator < 0 ) ^ ( denominator < 0 ):\n",
    "            res.append(\"-\")\n",
    "        \n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "\n",
    "        a, remain = divmod(numerator, denominator)\n",
    "        res.append(str(a))\n",
    "        if remain == 0: # 整除\n",
    "            return \"\".join(res)\n",
    "        res.append(\".\") # 小数\n",
    "\n",
    "        dic = {}\n",
    "        while remain != 0:\n",
    "            if remain in dic: #循环小数，添加括号\n",
    "                res.insert(dic[remain], \"(\")\n",
    "                res.append(\")\")\n",
    "                break\n",
    "            \n",
    "            dic[remain] = len(res)\n",
    "            remain *= 10\n",
    "            a, remain = divmod(remain, denominator)\n",
    "            res.append(str(a))\n",
    "        \n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        res = \"\"\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        s = (numerator > 0) is (denominator > 0)\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        ret = numerator // denominator\n",
    "        res += str(ret)\n",
    "        res = res if s else \"-\" + res\n",
    "        sub = numerator % denominator\n",
    "        if sub == 0:\n",
    "            return res\n",
    "        res += \".\"\n",
    "        a = dict()\n",
    "        a[sub] = res\n",
    "        while sub != 0:\n",
    "            res += str(sub * 10 // denominator)\n",
    "            sub = sub * 10 % denominator\n",
    "            if sub in a:\n",
    "                p = res.rfind(a[sub])\n",
    "                # print(p)\n",
    "                res = res[:p + len(a[sub])] + \"(\" + res[p + len(a[sub]):] + \")\"\n",
    "                return res\n",
    "            a[sub] = res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return \"0\"\n",
    "        self.curStr = \"\" if numerator * denominator > 0 else \"-\"\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        pre = numerator // denominator\n",
    "        self.curStr += str(pre)\n",
    "        self.subStr = \"\"\n",
    "        sub = numerator % denominator\n",
    "        if sub == 0:\n",
    "            return self.curStr\n",
    "        self.subList = [sub]\n",
    "        self.curStr += \".\"\n",
    "        self.getStr(sub * 10, denominator)\n",
    "        return self.curStr + self.subStr\n",
    "\n",
    "    def getStr(self, numerator, denominator):\n",
    "        pre = numerator // denominator\n",
    "        self.subStr += str(pre)\n",
    "        sub = numerator % denominator\n",
    "        if sub == 0:\n",
    "            return\n",
    "        if sub in self.subList:\n",
    "            idx = self.subList.index(sub)\n",
    "            self.subStr = self.subStr[:idx] + \"(\" + self.subStr[idx:] + \")\"\n",
    "            return\n",
    "        else:\n",
    "            self.subList.append(sub)\n",
    "            self.getStr(sub*10, denominator)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        flag = False\n",
    "        if numerator<0 and denominator<0:\n",
    "            numerator = -numerator\n",
    "            denominator= -denominator\n",
    "        elif numerator<0:\n",
    "            numerator = - numerator\n",
    "            flag = True\n",
    "        elif denominator < 0:\n",
    "            denominator = -denominator\n",
    "            flag = True\n",
    "        \n",
    "        number, denom = divmod(numerator, denominator)\n",
    "        if denom == 0:\n",
    "            result = str(number)\n",
    "            return result if not flag else '-' + result\n",
    "\n",
    "        result = str(number)\n",
    "        str_denom = ''\n",
    "        dict_denom = {}\n",
    "\n",
    "        print(numerator/denominator)\n",
    "        print(number,denom)\n",
    "        dict_denom[denom] = 0\n",
    "        index = 1\n",
    "\n",
    "        while index<1000:\n",
    "            num, denom = divmod(denom*10, denominator)\n",
    "            if denom==0:\n",
    "                str_denom += str(num)\n",
    "                break\n",
    "            print(index,num,denom,dict_denom)\n",
    "            str_denom += str(num)\n",
    "            width = len(str(num))\n",
    "            if num == 0:\n",
    "                dict_denom[denom] = index\n",
    "                index += width\n",
    "                continue\n",
    "            if denom in dict_denom:\n",
    "                print(denom,dict_denom[denom],index,width)\n",
    "                loop_str = str_denom[dict_denom[denom]:index+width]\n",
    "                print('loop:',loop_str,str_denom[:dict_denom[denom]])\n",
    "                result = result + '.' + str_denom[:dict_denom[denom]] + '(' + loop_str + ')'\n",
    "                return result if not flag else '-' + result\n",
    "            else:\n",
    "                dict_denom[denom] = index\n",
    "\n",
    "            index += width\n",
    "        result = result + '.' + str_denom\n",
    "        return result if not flag else '-' + result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "     def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator * denominator < 0:  # 判断正负\n",
    "            a = '-'\n",
    "            numerator = abs(numerator)\n",
    "            denominator = abs(denominator)\n",
    "        else:\n",
    "            a = ''\n",
    "\n",
    "        a += str(numerator // denominator)  # 将整数部分放到a中\n",
    "        numerator = numerator % denominator * 10\n",
    "\n",
    "        if numerator == 0:  # 判断是否为整数，整数就直接返回\n",
    "            return a\n",
    "\n",
    "        appeared = [numerator]  # 模拟小学除法，appeared为记录出现过的被除数\n",
    "        resultstr = ''  # resultstr记录小数部分\n",
    "        \n",
    "        while numerator != 0:  # 循环除\n",
    "            resultstr = resultstr + str(numerator // denominator)\n",
    "            numerator = numerator % denominator * 10\n",
    "            if numerator in appeared:  # 如果被除数出现过，证明从它上一次出现的位置产生了循环小数，跳出while循环\n",
    "                break\n",
    "            else:\n",
    "                appeared.append(numerator)\n",
    "        else:\n",
    "            return a + '.' + resultstr\n",
    "\n",
    "        place = appeared.index(numerator)  # index找到位置，并在两边加上括号即可\n",
    "        return a + '.' + resultstr[:place] + '(' + resultstr[place:] + ')'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator%denominator==0:\n",
    "            return str(int(numerator/denominator))\n",
    "        res=[]\n",
    "        if (numerator<0)!=(denominator<0):\n",
    "            res.append('-')\n",
    "        n=abs(numerator)\n",
    "        d=abs(denominator)\n",
    "        res.append(str(n//d))\n",
    "        res.append('.')\n",
    "        remainer=n%d\n",
    "        h={}\n",
    "        while remainer and remainer not in h:\n",
    "            h[remainer]=len(res)\n",
    "            remainer*=10\n",
    "            res.append(str(remainer//d))\n",
    "            remainer=remainer%d\n",
    "        if remainer!=0:\n",
    "            indexleft=h[remainer]\n",
    "            res.insert(indexleft,'(')\n",
    "            res.append(')')\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return \"0\"\n",
    "        numerator, denominator = denominator, numerator\n",
    "        flag = 1\n",
    "        if numerator < 0:\n",
    "            numerator = -numerator\n",
    "            flag *= -1\n",
    "        if denominator < 0:\n",
    "            denominator = -denominator\n",
    "            flag *= -1\n",
    "        flag = \"\" if flag >= 0 else \"-\"\n",
    "\n",
    "        integer, dec = divmod(denominator, numerator)\n",
    "        ret = flag + str(integer)\n",
    "        if not dec:\n",
    "            return ret\n",
    "\n",
    "        def gcd(a, b):\n",
    "            if a < b:\n",
    "                a, b = b, a\n",
    "            while a % b:\n",
    "                a = a % b\n",
    "                a, b = b, a\n",
    "            return b\n",
    "\n",
    "        ret += \".\"\n",
    "        gc = gcd(dec, numerator)\n",
    "        dec //= gc\n",
    "        numerator //= gc\n",
    "        integer2 = 1\n",
    "        slen = 0\n",
    "        while numerator % 10 == 0:  # 还有需要清理的数据\n",
    "            slen += 1\n",
    "            numerator //= 10\n",
    "        while numerator % 2 == 0:\n",
    "            slen += 1\n",
    "            integer2 *= 5\n",
    "            numerator //= 2\n",
    "        while numerator % 5 == 0:\n",
    "            slen += 1\n",
    "            integer2 *= 2\n",
    "            numerator //= 5\n",
    "        integer2, dec = divmod(dec * integer2, numerator)\n",
    "        if slen:\n",
    "            ret += \"0\" * (slen - len(str(integer2))) + str(integer2)\n",
    "        if not dec:\n",
    "            return ret\n",
    "\n",
    "        slen3 = 1\n",
    "        while dec * 10 ** slen3 % numerator != dec:\n",
    "            slen3 += 1\n",
    "        integer3 = dec * 10 ** slen3 // numerator\n",
    "        str3 = str(integer3)\n",
    "        return ret + \"(\" + \"0\" * (slen3 - len(str3)) + str3 + \")\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator==0:\n",
    "            return \"0\"\n",
    "        \n",
    "        res=[]\n",
    "        if (numerator>0) ^ (denominator>0):\n",
    "            res.append(\"-\")\n",
    "\n",
    "        numerator,denominator=abs(numerator),abs(denominator)\n",
    "        a,b=divmod(numerator,denominator)\n",
    "        res.append(str(a))\n",
    "\n",
    "        if b==0:\n",
    "            return \"\".join(res)\n",
    "        res.append('.')\n",
    "        loc={b:len(res)}\n",
    "        \n",
    "        while b:\n",
    "            b*=10\n",
    "            a,b=divmod(b,denominator)\n",
    "            res.append(str(a))\n",
    "            if b in loc:\n",
    "                res.insert(loc[b],\"(\")\n",
    "                res.append(\")\")\n",
    "                break\n",
    "            loc[b]=len(res)\n",
    "        return \"\".join(res)\n",
    "        \n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        pos = numerator*denominator>=0\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        ans = [str(numerator//denominator)] if pos else ['-'+str(numerator//denominator)]\n",
    "        if numerator % denominator:\n",
    "            ans.append('.')\n",
    "            temp = numerator % denominator\n",
    "            remainder = dict()\n",
    "            ind=2\n",
    "            while temp:\n",
    "                print(temp)\n",
    "                if temp not in remainder:\n",
    "                    remainder[temp] = ind\n",
    "                    temp *= 10\n",
    "                    ind += 1\n",
    "                    ans.append(str(temp//denominator))\n",
    "                    temp %= denominator\n",
    "                else:\n",
    "                    ans.insert(remainder[temp], '(')\n",
    "                    ans.append(')')\n",
    "                    break\n",
    "\n",
    "        return ''.join(ans) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        pn1 = ''\n",
    "        if numerator < 0:\n",
    "            pn1 = '-'\n",
    "            numerator = -numerator\n",
    "        pn2 = ''\n",
    "        if denominator < 0:\n",
    "            pn2 = '-'\n",
    "            denominator = -denominator\n",
    "        pn = ''\n",
    "        if pn1 != pn2:\n",
    "            pn = '-'\n",
    "\n",
    "        ip = numerator // denominator\n",
    "        numerator = (numerator%denominator) * 10\n",
    "        if numerator == 0:\n",
    "            return pn + str(ip)\n",
    "        \n",
    "        out = str(ip) + '.'\n",
    "        i = len(out)\n",
    "        numerators = {numerator:i-1}\n",
    "        while len(out) < 10000:\n",
    "            ip = numerator // denominator\n",
    "            numerator = (numerator%denominator) * 10\n",
    "            out += str(ip)\n",
    "            if numerator == 0:\n",
    "                return pn + out\n",
    "            try:\n",
    "                print(numerators, numerator)\n",
    "                if numerators[numerator] == 0:\n",
    "                    _p = numerators[numerator] + 2\n",
    "                else:\n",
    "                    _p = numerators[numerator] + 1    \n",
    "                return pn + f'{out[:_p]}({out[_p:]})'\n",
    "            except:\n",
    "                numerators[numerator] = i\n",
    "            i += 1\n",
    "                \n",
    "        return pn + out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        a,b = numerator, denominator\n",
    "        if a % b ==0:\n",
    "            return str(a//b)\n",
    "        \n",
    "        ans = ''\n",
    "        if a * b < 0:\n",
    "            ans+='-' \n",
    "        \n",
    "        a,b = abs(a), abs(b)\n",
    "\n",
    "        ans += str(a//b) + '.'\n",
    "\n",
    "        a %= b\n",
    "\n",
    "        hmap = {}\n",
    "        while a != 0:\n",
    "            if a in hmap:\n",
    "                u = hmap[a]  # 记录位置\n",
    "                return f\"{ans[:u]}({ans[u:]})\"\n",
    "            hmap[a] = len(ans)\n",
    "            a *= 10\n",
    "            ans += str(a // b)\n",
    "            a %= b\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        if numerator > 0 and denominator > 0 or numerator < 0 and denominator < 0:\n",
    "            sign = True\n",
    "        else:\n",
    "            sign = False\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        res = ''\n",
    "        remainder = numerator % denominator\n",
    "        integer = numerator // denominator\n",
    "        res = res + str(integer)\n",
    "        dicimalList = []\n",
    "        dicimal = ''\n",
    "        if remainder != 0:\n",
    "            res = res + '.'\n",
    "        while remainder != 0:\n",
    "            remainder = 10 * remainder\n",
    "            if remainder not in dicimalList:\n",
    "                dicimalList.append(remainder)\n",
    "            else:\n",
    "                position = dicimalList.index(remainder)\n",
    "                dicimal = dicimal[0:position] + '(' + dicimal[position:]\n",
    "                if sign:\n",
    "                    return res + dicimal + ')'\n",
    "                else:\n",
    "                    return '-' + res + dicimal + ')'\n",
    "            integer = remainder // denominator\n",
    "            remainder = remainder % denominator\n",
    "            dicimal = dicimal + str(integer)\n",
    "        if sign:\n",
    "            return res + dicimal\n",
    "        else:\n",
    "            return '-' + res + dicimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def fractionToDecimal(self, numerator: int, denominator: int) -> str:\n",
    "        if numerator == 0:\n",
    "            return '0'\n",
    "        if numerator >= 0 and denominator > 0 or numerator < 0 and denominator < 0:\n",
    "            sign = True\n",
    "        else:\n",
    "            sign = False\n",
    "        numerator = abs(numerator)\n",
    "        denominator = abs(denominator)\n",
    "        res = ''\n",
    "        remainder = numerator % denominator\n",
    "        integer = numerator // denominator\n",
    "        res = res + str(integer)\n",
    "        dicimalList = []\n",
    "        dicimal = ''\n",
    "        if remainder != 0:\n",
    "            res = res + '.'\n",
    "        while remainder != 0:\n",
    "            remainder = 10 * remainder\n",
    "            if remainder not in dicimalList:\n",
    "                dicimalList.append(remainder)\n",
    "            else:\n",
    "                position = dicimalList.index(remainder)\n",
    "                dicimal = dicimal[0:position] + '(' + dicimal[position:]\n",
    "                if sign:\n",
    "                    return res + dicimal + ')'\n",
    "                else:\n",
    "                    return '-' + res + dicimal + ')'\n",
    "            integer = remainder // denominator\n",
    "            remainder = remainder % denominator\n",
    "            dicimal = dicimal + str(integer)\n",
    "        if sign:\n",
    "            return res + dicimal\n",
    "        else:\n",
    "            return '-' + res + dicimal"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
